package cn.itsource.org.controller;

import cn.itsource.basic.PageList;
import cn.itsource.basic.annotation.PreAuthorize;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.LoginContext;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Controller
//@ResponseBody


//@GetMapping  获取数据
//@PostMapping 分页高级查询
//@PutMapping   新增或修改数据
//@DeleteMapping 删除数据
//写接口三要素：
//1.url是什么
//2.参数是什么
//3.返回值是什么

//复合注解
@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    @PutMapping
    @ApiOperation(value = "新增或修改部门" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        AjaxResult ajaxResult = new AjaxResult();
            //ctrl + alt + t  try catch代码块
            try {
                if(department.getId() != null){
                    departmentService.update(department);
                }else{
                    departmentService.add(department);
                }
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me().setMessage("操作失败!").setSuccess(false);
            }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryOne(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name="删除部门",value="dept:deleteById")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败!").setSuccess(false);
        }
    }

    @PostMapping
    @ApiOperation(value = "分页及高级查询" )
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(">>>>>>>>>>>>>>"+LoginContext.getCurrentUser(request).getUsername());
        return departmentService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败!").setSuccess(false);
        }
    }

    @GetMapping
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }
}
